﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BL;
using DA;
using Entities;

namespace ERP
{
    public partial class SupplierForm : BaseERPPage
    {
        SupplierManager supplierManager = new SupplierManager();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string SupplierId = Request.QueryString.Get("SupplierId");
                if (!string.IsNullOrEmpty(SupplierId))
                {
                    ERP_Suppliers supplier = supplierManager.GetSupplier(int.Parse(SupplierId));
                    txtAddress.Text = supplier.Address;
                    txtCity.Text = supplier.City;
                    txtVAT_Number.Text = supplier.VAT_Number.ToString();
                    txtName.Text = supplier.Name;
                    txtPhoneNumber.Text = supplier.Phone_Number;
                }
            }
        }

        protected void btn_click(object sender, EventArgs e)
        {
            string SupplierId = Request.QueryString.Get("SupplierId");
            if (!string.IsNullOrEmpty(SupplierId))  //Edit Supplier
            {
                if (supplierManager.EditSupplier(int.Parse(SupplierId), CollectSupplierDetails()))
                {
                    LabelAdded.Visible = true;
                }
            }

            else
            {//new order
                ERP_Suppliers Supplier = CollectSupplierDetails();
                if (supplierManager.AddSupplier(Supplier))
                {
                    LabelAdded.Visible = true;
                    Response.Redirect("OrderForm.aspx", true);

                }
            }
        }

        protected ERP_Suppliers CollectSupplierDetails()
        {
            ERP_Suppliers supplier = new ERP_Suppliers();
            supplier.Address = txtAddress.Text;
            supplier.City = txtCity.Text;
            supplier.VAT_Number = int.Parse(txtVAT_Number.Text);
            supplier.Name = txtName.Text;
            supplier.Phone_Number = txtPhoneNumber.Text;
            return supplier;
        }
    }
}